import docker,os,time,random


# client = docker.APIClient(base_url='unix://var/run/docker.sock')
# 获取当前系统正在运行的greaterwms镜像
def container():
    client = docker.from_env()
    #container = client.containers.list()
    local = client.images.list()
    print("拿到的本地镜像列表为：",local)
    name = client.containers.get('greaterwms_v2.0.25')
    print("获取到的本地正在运行的镜像id为:",name)
    return name
    #return imageid
    # client = docker.from_env()
    # docker_ps_list = client.containers()
    # length = len(docker_ps_list)
    # print(length)
    # for container in docker_ps_list:
    # print(container)
a = container()
print(type(a))


# 获取并分析特定镜像的日志
def logging():
    client = docker.from_env()
    containerid = container()
    print(type(containerid))
    lo = str(containerid.logs())
    time.sleep(10)

    #分析日志
    while True:
        sucuess = 'webpack output is served from'
        error = 'Error Unknown command'
        failed = 'failed'
        wait = 'Building fresh packages...'
        wait1 = 'Linking dependencies'
        flag = False
        print("等待前端依赖下载")
        b = random.randint(100,200)
        time.sleep(b)

        if lo.__contains__(sucuess):
            flag = True
            print("安装没有问题,可以访问了",flag)
            shell = os.system('docker ps -a')
            print(shell)
            break
        else :
            print("正在安装中，请稍等",flag)
            b = random.randint(100,200)
            time.sleep(b)
            if lo.__contains__(wait) or lo.__contains__(wait1):
                print("等待安装中")
                b = random.randint(100,200)
                time.sleep(b)
            if lo.__contains__(sucuess):
                flag = True
                print("安装没有问题",flag)
                break
            if lo.__contains__(failed):
                print("安装有问题，请重新安装",flag)
                shell = os.system('docker-compose down && docker-compose up -d')
                print("重装安装中")
                b = random.randint(100,200)
                time.sleep(b)
            if lo.__contains__(error):
                flag = False
                shell = os.system('docker-compose down && docker-compose up -d')
                b = random.randint(100,200)
                time.sleep(b)
i=1
for i in range(100):
    b = random.randint(100,200)
    c = logging()
    time.sleep(b)
    print(c)